package dao_.dao;

import jdbc.datasource.JDBCUtilsByDruid;
import jdbc.utils.JDBCUtils;
import org.apache.commons.dbutils.QueryRunner;
import org.apache.commons.dbutils.handlers.BeanHandler;
import org.apache.commons.dbutils.handlers.BeanListHandler;
import org.apache.commons.dbutils.handlers.ScalarHandler;

import java.sql.Connection;
import java.sql.SQLException;
import java.util.List;

//开发BasicDAO , 是其他DAO的父类
public class BasicDAO<T> {//泛型指定具体类型
    private QueryRunner queryRunner = new QueryRunner();

    //开发通用的dml方法, 针对任意的表
    public int update(String sql,Object... parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            int update = queryRunner.update(connection, sql, parameters);
            return update;
        } catch (SQLException e) {
            throw new RuntimeException(e);//将编译异常->运行异常 ,抛出
        } finally {
            JDBCUtilsByDruid.close(null,connection,null);
        }
    }

    /**
     * @param sql sql 语句，可以有 ?
     * @param tClass 传入一个类的Class对象 比如 Actor.class
     * @param parameters 传入 ? 的具体的值，可以是多个
     * @return 根据Actor.class 返回对应的 ArrayList 集合
     */
    //返回多个对象(即查询的结果是多行), 针对任意表
    public List<T> queryMulti(String sql,Class<T> tClass,Object... parameters){
        Connection connection = null;

        try {
            connection = JDBCUtilsByDruid.getConnection();
           return queryRunner.query(connection, sql, new BeanListHandler<T>(tClass), parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtilsByDruid.close(null,connection,null);
        }
    }
    //查询单行结果 的通用方法
    public T querySingle(String sql,Class<T> tClass,Object... parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return  queryRunner.query(sql, new BeanHandler<T>(tClass), parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtilsByDruid.close(null,connection,null);
        }
    }
    //查询单行单列的方法,即返回单值的方法
    public Object queryScalar(String sql,Object... parameters){
        Connection connection = null;
        try {
            connection = JDBCUtilsByDruid.getConnection();
            return queryRunner.query(connection, sql, new ScalarHandler(), parameters);
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } finally {
            JDBCUtilsByDruid.close(null,connection,null);
        }
    }
}
